'use strict';
app.controller('modulesAdvertisementEditCtrl',['$scope','$state','$stateParams','modulesAdvertisementEditService','utilService','toaster',function($scope,$state,$stateParams,modulesAdvertisementEditService,utilService,toaster){
    $scope.modulesId=$stateParams.modulesId;
    $scope.advertise = {
        id:$stateParams.id
    };
    $scope.imgQueue=[];
    $scope.limit=1;
    $scope.path=$scope.app.image.advertise;

    $scope.modulesAdvertisementEditService=modulesAdvertisementEditService;

    //查询分类类型
    $scope.getList=function(){
        modulesAdvertisementEditService.getList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.advertiseTypes = result.data;
                    break;
                case 633:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.getList();

    //根据id查询广告
    $scope.findById= function () {
        modulesAdvertisementEditService.findById($scope).then(function(result){
            $scope.saveImgQueue=new Array();
            switch (result.status){
                case 200:
                    $scope.advertise = result.data;

                    if($scope.advertise.imgUrl==null || $scope.advertise.imgUrl==''){
                        $scope.limit=1;
                    }else{
                        $scope.limit=0;
                        $scope.saveImgQueue.push($scope.advertise.imgUrl);
                    }

                    break;
                case 634:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.findById();

    //删除广告图片
    $scope.abledState=true;

    $scope.delImages=function(url,info){

        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;

        modulesAdvertisementEditService.delImages($scope,info.id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findById();
                    break;
                case 658:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }

            $scope.abledState=true;
        })
    }

    //修改广告
    $scope.editAdvertise = function() {
        if($scope.imgQueue.length>0){
            $scope.advertise.imgUrl=$scope.imgQueue[0].fileName;
        }

        modulesAdvertisementEditService.editAdvertise($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.modulesAdvertisement',{modulesId:$scope.modulesId});
                    break;
                case 634:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });

    }

}])